<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
?>
<script>
    var MasterBiayaStore = createStore(false, 'mMasterBiayaStore', ['kd_biaya', 'keterangan', 'id_jenis', 'jenis_keterangan', 'biaya_dasar', 'no_rek', 'rek_keterangan'], '<?php echo base_url(); ?>' + 'masterbiaya/get_rows');
    var JenisStore = createStore(false, 'mJenisStore', ['id_jenis','keterangan'], '<?php echo base_url(); ?>' + 'jenisbiaya/get_rows_combo');
    Ext.define('FormMBiy', {
        extend          : 'Ext.form.Panel',
        alias           : 'widget.mbiyForm',
        id              : 'FormMBiy_id',
        requires        : [
            'Ext.form.Field'
        ],
        defaultType     : 'textfield',
        defaults        : {
            allowBlank: false,
            labelAlign: 'left',
            labelWidth: 80
        },
        monitorValid: true,
        url: '<?php echo base_url(); ?>' + 'masterbiaya/update_rows',
        buttonAlign     : 'center',
        padding         : 5,
        style           : 'background-color: #fff;',
        border          : false,
        initComponent   : function(){
            this.items = [
                //                    cbjenis,                
                {
                    name: 'kd_biaya',
                    anchor:'90%',
                    id: 'mbiy_kd_biaya',
                    tooltip: 'Field tidak boleh kosong',
                    fieldLabel: 'Kode Biaya',
                    maxLength:10,
                    emptyText:'Auto Generate'
                    ,allowBlank:true
                }
                ,{
                    name: 'keterangan',
                    anchor:'90%',
                    id: 'mbiy_keterangan',
                    tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css,
                    fieldLabel: 'Keterangan',
//                    fieldStyle: 'text-transform:uppercase;',
                    maxLength:100,
                    allowBlank:false
//                    listeners: {
//                        change: function(field, newValue, oldValue) {
//                            field.setValue(newValue.toUpperCase());
//                        }
//                    }
                    
                },{
                    xtype: 'combo',                        
                    name:'id_jenis',
                    tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css,
                    fieldLabel: 'Jenis Biaya',                        
                    id: 'mbiy_id_jenis',
                    mode:'local',
                    store: JenisStore,
                    valueField: 'id_jenis',
                    displayField: 'keterangan',
                    typeAhead: true,
                    triggerAction: 'all',                    
                    allowBlank: false,
                    editable: false,
                    anchor: '90%',
                    hiddenName: 'id_jenis'
                    ,
                    emptyText: 'Jenis Biaya'              
                },
                ,{
                    xtype: 'numericfield',
                    name: 'biaya_dasar',
                    anchor:'90%',
//                    format: '0,0', 
                    id: 'mbiy_biaya_dasar',
                    tooltip: 'Field tidak boleh kosong',
                    hideTrigger: true,
                    keyNavEnabled: false,
                    mouseWheelEnabled: false,                    
                    afterLabelTextTpl: required_css,
                    fieldLabel: 'Biaya Dasar',
                    fieldStyle: 'text-transform:uppercase; text-align: right;',                    
                    minValue:0,       
                    value:0,
                    currencySymbol: null,
                                useThousandSeparator: true,
                                thousandSeparator: ',',
                                alwaysDisplayDecimals: false,
                    allowBlank:false

                }
            ];
            this.buttons = [
                {
                    text: 'Simpan',                    
                    itemId: 'simpan',
                    id:'btn_mbiy_simpan',
                    iconCls: 'save',
                    formBind: true,
                        scope: this,
                    handler: this.submit
                },
                {
                    text: 'Batal',
                    action: 'cancel',
                    itemId: 'batal',
                    iconCls: 'cancel',
                    handler: function(){
                        this.up('window').close();
                    }
                }
            ];
            this.callParent(arguments);
        },
        submit: function(){
            var parcmd=Ext.getCmp('btn_mbiy_simpan').getText();
            if(parcmd === 'Simpan'){
                parcmd='insert';
            }else if(parcmd === 'Edit'){
                parcmd='update';
            }
            var bi_dasar=Ext.getCmp('mbiy_biaya_dasar').getValue();
                    
            Ext.getCmp('FormMBiy_id').getForm().submit({
                url: this.url,
                scope: this,
                //                success: this.onSuccess,
                //                failure: this.onFailure,
                params: {
                    cmd: parcmd,
                    bi_dasar:bi_dasar
                },
                waitMsg: 'Saving Data...',
                success: function(form, action) {
                    //                    console.log(action);
                    set_message(0,action.result.msg);
                    //                    Ext.Msg.alert('Success', action.result.msg);
                    Ext.getCmp('gridmbiy').store.reload(); 
                    Ext.getCmp('FormMBiy_id').getForm().reset();
                    Ext.getCmp('WindowMBiy_id').close();
                },
                failure: function(form, action) {
                    if(action.result.msg=='Session Expired') {
                        session_expired(action.result.msg);
                    }else{
                        set_message(1, action.result.msg);
                    }
                                            
                    
                }
            });
        } // eo function submit        
        ,
        showError: function(msg, title){
            title = title || 'Error';
            Ext.Msg.show({
                title: title,
                msg: msg,
                modal: true,
                icon: Ext.Msg.ERROR,
                buttons: Ext.Msg.OK,
                fn: function(btn){
                    if (btn == 'ok' && msg == 'Session Expired') {
                        window.location = '<?= site_url("auth/login") ?>';
                    }
                }
            });
        }
    });

    Ext.define('WindowMBiy', {
        extend          : 'Ext.window.Window',
        title           : 'Form Edit User',
        width           : 400,
        height          : 180,
        layout          : 'fit',
        autoShow        : true,
        modal           : true,
        alias           : 'widget.MJbiEdit',
        id              : 'WindowMBiy_id',
        initComponent   : function(){
            this.items = [
                Ext.widget('mbiyForm')
            ];
            this.callParent(arguments);
        }

    });
      
    Ext.define('MyTabMasterBiaya', {
        extend: 'Ext.container.Container',
        xtype: 'TabMasterBiaya',
        alias: 'widget.TabMasterBiaya',
        title: 'Master Biaya',
        id: 'tab5',
        closable: true,        
        layout: 'border',
        items: [{
                xtype: 'panel',
                autoShow: true,
                id: 'panelmastermbiy',            
                region: 'center',
                margins: '5 5 5 5',
                layout: 'fit',
                items:[
                    {
                        xtype:'grid',
                        id:'gridmbiy',
                        stateful:true,
                        stateId:'stateGridMBiy',
                        store: MasterBiayaStore,//Ext.data.StoreManager.lookup('mMasterAccountStore'),
                        stripeRows: true,
                        loadMask: true,
                        //                        sm:sm_masteraccount,
                        columns:[
                            {
                                header: 'Edit/Delete',
                                menuDisabled: true,
                                sortable: false,
                                xtype: 'actioncolumn',
                                width: 70,
                                items: [
                                    {
                                        iconCls: 'icon-edit-record',
                                        tooltip: 'Edit Row',
                                        handler: function(grid, rowIndex, colIndex) {
                                            var rec = grid.getStore().getAt(rowIndex);
                                            var winmkls=Ext.create('WindowMBiy');
                                            winmkls.setTitle('Edit Form');
                                            Ext.getCmp('btn_mbiy_simpan').setText('Edit');
                                            Ext.getCmp('btn_mbiy_simpan').setIconCls('icon-edit-record');
                                            Ext.getCmp('mbiy_kd_biaya').setReadOnly(true);
                                            Ext.getCmp('mbiy_kd_biaya').setFieldStyle('readonly-input');
                                            Ext.getCmp('mbiy_kd_biaya').setValue(rec.get('kd_biaya'));
                                            Ext.getCmp('mbiy_id_jenis').setValue(rec.get('id_jenis'));
                                            Ext.getCmp('mbiy_keterangan').setValue(rec.get('keterangan'));                                            
                                            Ext.getCmp('mbiy_biaya_dasar').setValue(rec.get('biaya_dasar'));
                                            winmkls.show();
                                            //                                            Ext.Msg.alert('Edit', 'Edit ' + rec.get('rekening'));
                                        }
                                    },{
                                        getClass: function(v, meta, rec) {
                                            return 'icon-delete';
                                        },
                                        getTip: function(v, meta, rec) {
                                            return 'Delete Plant';
                                        },
                                        handler: function(grid, rowIndex, colIndex) {
                                            var rec = grid.getStore().getAt(rowIndex);
//                                            console.log(rec.data);
                                            //                                            Ext.Msg.alert('Delete', 'Delete ' + rec.get('rekening'));
                                            Ext.Msg.show({
                                                title: 'Confirm',
                                                msg: 'Are you sure delete selected row ?',
                                                buttons: Ext.Msg.YESNO,
                                                icon: Ext.Msg.QUESTION,
                                                fn: function(btn){
                                                    if (btn == 'yes') {
                    
                                                        var data = '';
                                                        data = Ext.JSON.encode(rec.data);
                        
                                                        Ext.Ajax.request({                                                            
                                                            url: '<?php echo base_url(); ?>' +'masterbiaya/delete_row',
                                                            method: 'POST',
                                                            params: {
                                                                cmd: 'delete',
                                                                postdata: data
                                                            },
                                                            success: function(obj) {
                                                                var   resp = Ext.decode(obj.responseText);                                                                
                                                                if(resp.success==true){
                                                                    //                                                                    Ext.Msg.alert('info',resp.msg);
                                                                    set_message(0,resp.msg);
                                                                    Ext.getCmp('gridmbiy').store.reload();                                                                                                                                     
                                                                }else{
                                                                    Ext.Msg.show({
                                                                        title: 'Error',
                                                                        msg: resp.msg,
                                                                        modal: true,
                                                                        icon: Ext.Msg.ERROR,
                                                                        buttons: Ext.Msg.OK,
                                                                        fn: function(btn){
                                                                            if (btn == 'ok' && resp.msg == 'Session Expired') {
                                                                                window.location = '<?= site_url("auth/login") ?>';
                                                                            }
                                                                        }
                                                                    });
                                                                }
                                                            },
                                                            failure: function(obj) {
                                                                var  resp = Ext.decode(obj.responseText);
                                                                set_message(0, resp.reason);
                                                                //                                                                Ext.Msg.alert('info',resp.reason);
                                                            }                                                            
                                                        });                 
                                                    } 
                                                }
                                            });
                                        }
                                    }
                                ]
                            }
                           
                            ,
                            {
                                header: "Kode Biaya",
                                dataIndex: 'kd_biaya',
                                sortable: true,
                                width: 100
                            }
                            ,{
                                header: "Keterangan",
                                dataIndex: 'keterangan',
                                sortable: true,
                                width: 150
                            },{
                                xtype:'numbercolumn',
                                text: "Biaya Dasar",
                                dataIndex: 'biaya_dasar',
                                sortable: true,
                                width: 150,
                                align:'right',
                                format: '0,0'
                            },
                            {
                                header: "Jenis Biaya",
                                dataIndex: 'id_jenis',
                                sortable: true,
                                width: 100
                            }
                            ,{
                                header: "Keterangan Jenis",
                                dataIndex: 'jenis_keterangan',
                                sortable: true,
                                width: 150,
                                flex:1
                            }
                            ,{
                                header: "Rekening",
                                dataIndex: 'no_rek',
                                sortable: true,
                                width: 100
                            }
                            ,{
                                header: "Keterangan Rekening",
                                dataIndex: 'rek_keterangan',
                                sortable: true,
                                width: 150,
                                flex:1
                            }
                        
                        ]
                        ,tbar:{
                            xtype: 'toolbar',
                            padding:'2 0 2 5',
                            items: [
                                {
                                    xtype: 'button',
                                    text: 'Add',
                                    iconCls: 'icons-add',
                                    onClick: function()
                                    {
                                        var winmkls=Ext.create('WindowMBiy');
                                        winmkls.setTitle('Add Form');
                                        Ext.getCmp('btn_mbiy_simpan').setText('Simpan');
                                        Ext.getCmp('btn_mbiy_simpan').setIconCls('icons-add');
                                        Ext.getCmp('mbiy_kd_biaya').setReadOnly(true);
                                        Ext.getCmp('mbiy_kd_biaya').setFieldStyle('readonly-input');
                                        //                                        setDefaultStoreProxy(KelompokAccountStore, urlKelompok);
                                        winmkls.show();
                                    } 
                                },
                                '-',
                                {
                                    width: 300,
                                    //                                fieldLabel: 'Search',
                                    //                                labelWidth: 50,
                                    xtype: 'searchfield',
                                    store: MasterBiayaStore,
                                    emptyText: 'Quick Search...'
                                }
                            ]
                        }
                        ,bbar:{                        
                            xtype: 'pagingtoolbar',
                            store: MasterBiayaStore,pageSize: ENDPAGE,
                            displayInfo: true
                            //                    plugins: Ext.create('Ext.ux.SlidingPager', {})
                        }

                    }
                ]
            
            }
        
        ],
        listeners:{
            show:function(){
                var storegrid=Ext.getCmp('gridmbiy').store;                
                storegrid.loadPage(1);
                JenisStore.load();
                //                JenisAccountStore.load();
                //                KelompokAccountStore.load();    
                
            }
        }
        , initComponent: function() {
            this.callParent(arguments);
        }
    });

</script>